package com.cevent.cms.controller.admin;

import com.cevent.common.domain.ModuleBase;
import com.cevent.common.dto.ModuleBaseChildrenDto;
import com.cevent.common.dto.ModuleBaseDto;
import com.cevent.common.dto.PageDto;
import com.cevent.common.dto.RespDto;
import com.cevent.common.service.ModuleBaseService;
import com.cevent.common.util.UuidUtil;
import com.cevent.common.util.ValidatorUtil;
import com.cevent.common.exception.ValidatorException;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
   @description 模块基础表
 * @author cevent
 * @create 2025-03-31 15:03:19
 */
@RestController
@RequestMapping("/admin/moduleBase")
public class ModuleBaseController {

    public static final String SERVER_NAME="模块基础表";

    // 快捷键logf
    private static final Logger LOG = LoggerFactory.getLogger(ModuleBaseController.class);

    @Resource
    private ModuleBaseService moduleBaseService;

    /**
     * 分页配置
     *
     * @param pageDto 接受表单方式
     * @param domain  配置header可以没有参数，required=false
     * @return
     */
    @PostMapping("/pageData")
    public RespDto getList(@RequestBody PageDto pageDto, @RequestHeader(value = "domain", required = false) String domain) {
        LOG.info("pageDto: {},domain: {}", pageDto, domain);
        moduleBaseService.getList(pageDto, domain);
        RespDto respDto = new RespDto();
        respDto.setContent(pageDto);
        return respDto;
    }

    @RequestMapping("/tree")
    public RespDto getTree(@RequestHeader(value = "domain",required = false) String domain){
        List<ModuleBaseChildrenDto> categoryChildrenDtos=moduleBaseService.getTree(domain);

        RespDto respDto=new RespDto();
        if(categoryChildrenDtos.size()>0){
            respDto.setContent(categoryChildrenDtos);
        }else{
            respDto.setSuccess(false);
            respDto.setMessage("暂无分类数据，请联系后台进行维护");
        }

        return respDto;
    }

    @PostMapping("/save")
    public RespDto save(@RequestBody ModuleBaseDto moduleBaseDto, @RequestHeader(value = "domain", required = false) String domain) {
        RespDto respDto = new RespDto();

        ValidatorUtil.isEmpty(moduleBaseDto.getParentId(), "父级ID");
        ValidatorUtil.isEmpty(domain, "域名");
        ValidatorUtil.isEmpty(moduleBaseDto.getLevel(), "模块等级");
        ValidatorUtil.isEmpty(moduleBaseDto.getSort(), "排序");
        ValidatorUtil.isEmpty(moduleBaseDto.getIsShow(), "是否显示");


        moduleBaseService.save(moduleBaseDto, domain);

        respDto.setContent(moduleBaseDto);
        return respDto;
    }

    @PostMapping("saveTree")
    public RespDto saveTree(@RequestBody List<ModuleBaseChildrenDto> childrenDtos,@RequestHeader(value = "domain",required = false) String domian){

        RespDto respDto=new RespDto();

        respDto.setContent(moduleBaseService.saveTree(childrenDtos,domian));

        return respDto;
    }

    @GetMapping("/detail/{uniId}")
    public RespDto detail(@PathVariable String uniId) {
        ModuleBaseDto moduleBaseDto = moduleBaseService.detail(uniId);
        RespDto respDto = new RespDto();
        if (moduleBaseDto == null) {
            respDto.setSuccess(false);
        }
        respDto.setContent(moduleBaseDto);
        return respDto;
    }

    @DeleteMapping("/delete/{uniId}")
    public RespDto delete(@PathVariable String uniId, @RequestHeader(value = "domain", required = false) String domain) {
        Boolean flag = moduleBaseService.delete(uniId, domain);
        RespDto respDto = new RespDto();
        respDto.setSuccess(flag);
        return respDto;
    }

}
